Core = {
	alert: function(message) {
		alert(message);
	},
	
	loadTrustedModule: function(module) {
		var message = 'A widget is requesting permission to load the trusted module "' + module.name + '"';
		message += 'A trusted module has full access to the page it runs on.';
		message += 'This means it has access to your login information, can automate actions you can do manually, and various other things.';
		message += 'It requests the following permissions:\n';
		
		for (c in module.permissions) {
			message += '- ' + module.permissions[c] + '\n';
		}
		
		message += '\nDo you wish to allow this module to run?';
		
		if (confirm(message)) {
			if (confirm('Would you like to remember this setting?')) {
				//save pref.
			}
			else {
				$.getScript(module.script);
			}
		}
	}
};

Thread = {
	_toolsMenu: null,
	
	quickReply: function(message) {
		//whether to use rich text editor iframe or the textarea.
		if ($('#vB_Editor_QR_iframe').is(':visible')) {
			message = message.replace('\n', '<br>');
			$('#vB_Editor_QR_iframe')[0].contentWindow.document.execCommand('insertHTML', null, message);
		}
		else {
			$('#vB_Editor_QR_textarea').val(message);
		}
		
		$('#qr_submit').click();
	},
	
	getID: function() {
		var href = location.href;
		var end = href.lastIndexOf('/');
		
		//Handle pages (ex: t12345-2, for page 2 of id t12345)
		var pageSplit = href.lastIndexOf('-', end);
		if (pageSplit != -1) {
			end = pageSplit;
		}
		
		var begin = href.lastIndexOf('/', end - 1) + 1;
		var threadID = href.substring(begin, end);
		return threadID;
	},
	
	setPrefix: function(prefix, callback) {
		if (!callback) callback = function() {};
		if (Thread._toolsMenu == null) {
			Thread._toolsMenu = $('#threadtools_menu form[name="threadadminform"]');
		}
		
		//Load/create proxy iframe for opening the edit thread interface.
		var proxyFrame = $('#proxyFrame');
		if (proxyFrame.length == 0) {
			proxyFrame = $('<iframe name="proxyFrame" id="proxyFrame"></iframe>');
			proxyFrame.css('display', 'none');
			proxyFrame.css('height', '0px');
			proxyFrame.css('width', '0px');
			$('body').append(proxyFrame);
		}
		
		//Hijacks (then de-hijacks) the thread tools menu
		//in order to load the iframe.
		with ({ menu : Thread._toolsMenu }) {
			menu.attr('target', 'proxyFrame');
			menu.find(':radio[value="editthread"]').click();
			menu.submit();
			menu.removeAttr('target');
		}
		
		//Set the prefix.
		proxyFrame.load(function() {
			var proxyForm = $(this).contents().find('form[name="vbform"]');
			$('select[name="prefixid"]', proxyForm).val(prefix);
			proxyForm.ajaxSubmit(callback);				
		});
	}
	
};

Page = {
	ifIsThread: function() {
		if (location.href.match(/www\.christianforums\.com\/t+/)) {
			return true;
		}
		else {
			return false;
		}
	},
	
	ifIsReport: function() {
		if (Page.ifIsThread()) {
			var found = false;
			$('span[class="navbar"]').children('a').each(function() {
				if ($(this).html() == 'Main Report Forum' || $(this).html() == 'Recovery Category Reports') {
					found = true;
					return false; //breaks out of the foreach.
				}
			});
			
			return found;
		}
		else {
			return false;
		}
	},
	
	addCssFile: function(href) {
        var css = $('<link></link>');
        css.attr('href', href);
        css.attr('rel', 'stylesheet');
        css.attr('type', 'text/css');
        $('head').append(css);
	}
};

/* Internal functions for bridging the gap between global and widget */
__wwBridge = {
	match: function(pattern) {
		if (location.href.match(pattern).length > 0) {
			return true;
		}
		else {
			return false;
		}
	}
};

__wwBridge.Cloud = {
	hostedFrame: function(options) {
		var filters = { first: $.fn.first, last: $.fn.last };
		var placements = { prepend: $.fn.prepend, append: $.fn.append };
		
		var frame = $('<iframe></iframe>');
			
		frame.attr('src', options.url);
		frame.attr('id', options.id);
		
		//Find css in remaining options
		for (var option in options) {
			if (option != 'id' && option != 'url' && option != 'filter' && option != 'placement' && option != 'selector') {
				frame.css(option, options[option]);
			}
		}
			
		//Safe DOM mods to apply to this frame.
		var filter, placement, wrap;
		
		//Discover the filter function (to shorten down the list of selected elements)
		if (options.filter) {
			if (filters[options.filter]) {
				filter = filters[options.filter];
			}
			else {
				filter = filters['first'];
			}
		}
		else {
			filter = filters['first'];
		}
		
		//Figure out where to place the fra,e/
		if (options.placement) {
			if (placements[options.placement]) {
				placement = placements[options.placement];
			}
			else {
				placement = placements['append'];
			}
		}
		else {
			placement = placements['append'];
		}
		
		//Find the element(s) according to the selector.
		var el = $(options.selector);
		
		//Perform all DOM mods
		el = filter.call(el); 
		placement.call(el, frame);
		if (options.wrap) {
			el.wrap(options.wrap);
		}
		
		this.exposeSelector('#' + options.id);
		//Replace later with some wrapped thingy that allows hosted API interaction.
		return true;
	}
};

__wwBridge.UI = {
	freeFloatButton: function(options) {
		var btn = $('<div id="' + options.id + '">' + options.innerText + '</div>');
		btn.addClass('freeFloatButton');
		
		//Find css in remaining options
		for (var option in options) {
			if (option != 'id' && option != 'innerText') {
				btn.css(option, options[option]);
			}
		}
		
		//Add button
		$('body').append(btn);
		this.exposeSelector('#' + options.id);
	}
};